package offer;

import java.util.LinkedList;

/**
 * 关键在于只有satckPop为空时,才能把stackPush全部压入pop 完成逆序
 * @author fance
 * @date 18-5-1 上午9:47
 */
public class TwoStackQueue {
    private LinkedList<Integer> stackPush;
    private LinkedList<Integer> stackPop;

    public TwoStackQueue(LinkedList<Integer> stackPush, LinkedList<Integer> stackPop) {
        this.stackPush = stackPush;
        this.stackPop = stackPop;
    }

    public void offer(int val) {
        stackPush.push(val);
    }

    public int poll() {
        if (stackPop.isEmpty() && stackPush.isEmpty()) {
            throw new RuntimeException("queue is empty");
        } else if (stackPop.isEmpty()){
            while (!stackPush.isEmpty()) {
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.pop();
    }

    public int peek() {
        if (stackPop.isEmpty() && stackPush.isEmpty()) {
            throw new RuntimeException("queue is empty");
        } else if (stackPop.isEmpty()){
            while (!stackPush.isEmpty()) {
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.peek();
    }
}
